import { parse } from 'url';
import webpath from '@/utils/webpath';

// mock tableListDataSource
let tableListDataSource = [];
for (let i = 0; i < 46; i += 1) {
    tableListDataSource.push({
        id: `李海波 id ${i}`,
        opername: `李海波 opername ${i}`,
        opertype: `李海波 opertype ${i}`,
        class_method: `李海波 class_method ${i}`,
        http_method: `李海波 http_method ${i}`,
        http_url: `李海波 http_url ${i}`,
        http_params: `李海波 http_params ${i}`,
        http_body: `李海波 http_body ${i}`,
        http_header: `李海波 http_header ${i}`,
        content: `李海波 content ${i}`,
        cost: `李海波 cost ${i}`,
        createip: `李海波 createip ${i}`,
        createuser: `李海波 createuser ${i}`,
        createdate: `李海波 createdate ${i}`,
        memo: `李海波 memo ${i}`,
        client_type: `李海波 client_type ${i}`,
        status: Math.floor(Math.random() * 10) % 4,
    });
}

function getProOperlog(req, res, u) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const params = parse(url, true).query;

    let dataSource = tableListDataSource;

    if (params.sorter!=undefined) {
        const s = params.sorter.split('_');
        dataSource = dataSource.sort((prev, next) => {
            if (s[1] === 'descend') {
            return next[s[0]] - prev[s[0]];
        }
        return prev[s[0]] - next[s[0]];
    });
    }

    if (params.status!=undefined) {
    const status = params.status.split(',');
    let filterDataSource = [];
    status.forEach(s => {
    filterDataSource = filterDataSource.concat(
    dataSource.filter(data =>
    {

        if(data.status!=undefined){
        return parseInt(data.status, 10) === parseInt(s[0], 10);
        }
    })
    );
    });
    dataSource = filterDataSource;
    }

    if (params.name!=undefined) {
    dataSource = dataSource.filter(data => {

        if(data.name!=undefined){
        return data.name.indexOf(params.name) > -1;
        }

    });
    }

    let pageSize = 10;
    if (params.pageSize!=undefined) {
        pageSize = params.pageSize * 1;
    }

    const result = {
        list: dataSource,
        pagination: {
            total: dataSource.length,
            pageSize,
            current: parseInt(params.currentPage, 10) || 1,
        },
    };

    return res.json(result);
}



function deleteByPrimaryKeyForProOperlog(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;

    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



    return getProOperlog(req, res, u);
}


function selectByPrimaryKeyForProOperlog(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProOperlog(req, res, u);
}



function insertForProOperlog(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;



    const i = Math.ceil(Math.random() * 10000);
    tableListDataSource.unshift({
        id: `李海波 id ${i}`,
        opername: `李海波 opername ${i}`,
        opertype: `李海波 opertype ${i}`,
        class_method: `李海波 class_method ${i}`,
        http_method: `李海波 http_method ${i}`,
        http_url: `李海波 http_url ${i}`,
        http_params: `李海波 http_params ${i}`,
        http_body: `李海波 http_body ${i}`,
        http_header: `李海波 http_header ${i}`,
        content: `李海波 content ${i}`,
        cost: `李海波 cost ${i}`,
        createip: `李海波 createip ${i}`,
        createuser: `李海波 createuser ${i}`,
        createdate: `李海波 createdate ${i}`,
        memo: `李海波 memo ${i}`,
        client_type: `李海波 client_type ${i}`,
        status: Math.floor(Math.random() * 10) % 4,
    });


    return getProOperlog(req, res, u);
}




function updateForProOperlog(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;

    if(id!=undefined)
    tableListDataSource = tableListDataSource.map(item => {
        if (item.id === id) {
        Object.assign(item, { desc, name });
        return item;
    }
    return item;
});


    return getProOperlog(req, res, u);
}


function deleteByMapForProOperlog(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProOperlog(req, res, u);
}



function updateByMapForProOperlog(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProOperlog(req, res, u);
}

function getOneForProOperlog(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(undefined!=id)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



    return getProOperlog(req, res, u);
}

function getAllForProOperlog(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



    return getProOperlog(req, res, u);
}

function queryPageForProOperlog(req, res, u, b) {


    return getProOperlog(req, res, u);
}

function countForProOperlog(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProOperlog(req, res, u);
}


function insertBatchForProOperlog(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProOperlog(req, res, u);
}



function deleteBatchForProOperlog(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProOperlog(req, res, u);
}


function updateBatchForProOperlog(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProOperlog(req, res, u);
}




export default {
    'GET /${webpath}/web/api/service/prooperlog/delete': deleteByPrimaryKeyForProOperlog,
    'GET /${webpath}/web/api/service/prooperlog/get': selectByPrimaryKeyForProOperlog,
    'POST /${webpath}/web/api/service/prooperlog/insert': insertForProOperlog,
    'POST /${webpath}/web/api/service/prooperlog/update': updateForProOperlog,
    'POST /${webpath}/web/api/service/prooperlog/deleteByMap': deleteByMapForProOperlog,
    'POST /${webpath}/web/api/service/prooperlog/updateByMap': updateByMapForProOperlog,
    'POST /${webpath}/web/api/service/prooperlog/getOne': getOneForProOperlog,
    'POST /${webpath}/web/api/service/prooperlog/getAll': getAllForProOperlog,
    'POST /${webpath}/web/api/service/prooperlog/queryPage': queryPageForProOperlog,
    'POST /${webpath}/web/api/service/prooperlog/count': countForProOperlog,
    'POST /${webpath}/web/api/service/prooperlog/insertBatch': insertBatchForProOperlog,
    'POST /${webpath}/web/api/service/prooperlog/deleteBatch': deleteBatchForProOperlog,
    'POST /${webpath}/web/api/service/prooperlog/updateBatch': updateBatchForProOperlog,
};
